/**
 * Created by ankang on 2017/2/21.
 */
$(function () {
    // queryProfession();
    // queryProvince(0, "", 1);
    // queryTaxProvince(0, "CN", 2);
    // queryTaxProvince(0, "CN", 3);
    // queryCity(1);
    // queryCity(2);
    // queryCity(3);
});
var tax_type = "";
var sel_con1 = "";
var sel_con2 = "";
var sel_prov1 = "";
var sel_prov2 = "";
var sel_city1 = "";
var sel_city2 = "";
//查询职业
function queryProfession() {
    $.ajax({
        type: 'post',
        timeout: 60000,
        url: ajaxUrl() + "queryProfession",
        data: {
            merchantId: merchantId,
            channel: channel,
            version: version,
            uuid: uuid,
            Mac: Mac,
            user_type: user_type,
            platform: platform,
            phone_type: phone_type,
            appChannel: appChannel,
        },
        dataType: 'json',
        success: function (data) {
            //hideLoading();
            var a = data.data;
            var professionTypeList = ""; //职业列表
            if (data.respCode == "0000") {
                $("#professionType").html("");
                $("#professionType").append('<option value="">请选择职业</option>');
                $(a).each(function (i, n) {
                    professionTypeList += '<option value="' + n.professionCode + '">' + n.professionName + '</option>';
                });
                $("#professionType").append(professionTypeList);
                $("#professionType").mobiscroll().select({
                    theme: 'mobiscroll',
                    display: 'bottom',
                    headerText: function (valueText) {
                        return "请选择职业";
                    }
                });
            } else {
                setErrorMsg(data.respCode, data.respMsg);
            }
        },
        error: function (data) {
            //hideLoading();
            if (data.statusText == "timeout") {
                errorShowAlert("请求超时");
            } else {
                errorShowAlert("请求失败，请稍后再试。");
            }
        }
    })
}

//查询省
function queryProvince(queryType, provinceCode, index) {
    //hideLoading();
    //showLoading();
    $.ajax({
        type: 'post',
        timeout: 60000,
        url: ajaxUrl() + "queryProvinceCity",
        data: {
            merchantId: merchantId,
            channel: channel,
            uuid: uuid,
            version: '3.0.9',
            Mac: Mac,
            user_type: user_type,
            platform: platform,
            phone_type: phone_type,
            appChannel: appChannel,
            queryType: queryType,
            provinceCode: provinceCode
        },
        dataType: 'json',
        success: function (data) {
            //hideLoading();
            var a = data.data;
            var provinceTypeList = ""; //省列表
            if (data.respCode == "0000") {
                $("#provinceType" + index).html("");
                $("#provinceType" + index).append('<option value="">请选择省</option>');
                $(a).each(function (i, n) {
                    provinceTypeList += '<option value="' + n.provinceCode + '">' + n.provinceName + '</option>';
                });
                $("#provinceType" + index).append(provinceTypeList);
                $("#provinceType" + index).mobiscroll().select({
                    theme: 'mobiscroll',
                    display: 'bottom',
                    headerText: function (valueText) {
                        return "请选择省";
                    },
                    onSelect: function (valueText, inst) {
                        var proCode = $(this).val();
                        if (proCode) {
                            queryCity(index, proCode);
                        }
                    }
                });
            } else {
                setErrorMsg(data.respCode, data.respMsg);
            }
        },
        error: function (data) {
            //hideLoading();
            if (data.statusText == "timeout") {
                errorShowAlert("请求超时");
            } else {
                errorShowAlert("请求失败，请稍后再试。");
            }
        }
    })
}
//涉税--查询省
function queryTaxProvince(queryType, provinceCode, index) {
    //hideLoading();
    //showLoading();
    var objTaxProvince = {
        merchantId: merchantId,
        channel: channel,
        uuid: uuid,
        Mac: Mac,
        user_type: user_type,
        platform: platform,
        phone_type: phone_type,
        appChannel: appChannel,
        queryType: queryType,
        provinceCode: provinceCode
    }
    $.ajax({
        type: 'post',
        timeout: 60000,
        url: ajaxUrlNew() + "smartfund/queryTaxProvince",
        data: JSON.stringify(objTaxProvince),
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
            var queryTaxInfoParams = JSON.parse(sessionStorage.getItem('queryTaxInfoParams'));
            var a = data.data;
            var provinceTypeList = ""; //省列表
            if (data.errorCode == "0000") {
                //$("#provinceType" + index).html("");
                $("#provinceType" + index).append('<option value="">请选择省</option>');
                $(a).each(function (i, n) {
                    provinceTypeList += '<option value="' + n.regionCode + '">' + n.regionNameCn + '</option>';
                });
                $("#provinceType" + index).append(provinceTypeList);
                if (queryTaxInfoParams) {
                    if (queryTaxInfoParams.domicileProvinceCn) {
                        $("#provinceType2").val(queryTaxInfoParams.domicileProvinceCn);
                    }
                    if (queryTaxInfoParams.birthplaceProvinceCn) {
                        $("#provinceType3").val(queryTaxInfoParams.birthplaceProvinceCn);
                    }
                }
                $("#provinceType" + index).mobiscroll().select({
                    theme: 'mobiscroll',
                    display: 'bottom',
                    headerText: function (valueText) {
                        return "请选择省";
                    },
                    onSelect: function (valueText, inst) {
                        sel_prov1 = $('#provinceType2').find("option:selected").val();
                        localStorage.sel_prov1 = sel_prov1 || localStorage.sel_prov1;
                        sel_prov2 = $('#provinceType3').find("option:selected").val();
                        localStorage.sel_prov2 = sel_prov2 || localStorage.sel_prov2;
                        var proCode = $(this).val();
                        if (proCode) {
                            queryTaxCity(index, proCode);
                        }
                    }
                });
            } else {
                setErrorMsg(data.respCode, data.respMsg);
            }
        },
        error: function (data) {
            //hideLoading();
            if (data.statusText == "timeout") {
                errorShowAlert("请求超时");
            } else {
                errorShowAlert("请求失败，请稍后再试。");
            }
        }
    })
}
//查询市
function queryCity(index, provinceCode) {
    hideLoading();
    showLoading();
    $.ajax({
        type: 'post',
        timeout: 60000,
        url: ajaxUrl() + "queryProvinceCity",
        data: {
            merchantId: merchantId,
            channel: channel,
            version: '3.0.9',
            uuid: uuid,
            Mac: Mac,
            user_type: user_type,
            platform: platform,
            phone_type: phone_type,
            appChannel: appChannel,
            queryType: 1,
            provinceCode: provinceCode
        },
        dataType: 'json',
        success: function (data) {
            hideLoading();
            var b = data.data;
            var cityTypeList = ""; //市列表
            if (data.respCode == "0000") {
                $("#cityType" + index).html("");
                $("#cityType" + index).append('<option value="">请选择市/区城市</option>');
                $(b).each(function (i, m) {
                    cityTypeList += '<option value="' + m.cityCode + '">' + m.cityName + '</option>';
                });
                $("#cityType" + index).append(cityTypeList);
                $("#cityType" + index).mobiscroll().select({
                    theme: 'mobiscroll',
                    display: 'bottom',
                    headerText: function (valueText) {
                        return "请选择市/区城市";
                    },
                    onSelect: function () {
                    }
                });
            } else {
                setErrorMsg(data.respCode, data.respMsg);
            }
        },
        error: function (data) {
            hideLoading();
            if (data.statusText == "timeout") {
                errorShowAlert("请求超时");
            } else {
                errorShowAlert("请求失败，请稍后再试。");
            }
        }
    })
}
//涉税--查询市
function queryTaxCity(index, provinceCode) {
    hideLoading();
    showLoading();
    var objTaxCity = {
        merchantId: merchantId,
        channel: channel,
        version: '3.0.9',
        uuid: uuid,
        Mac: Mac,
        user_type: user_type,
        platform: platform,
        phone_type: phone_type,
        appChannel: appChannel,
        queryType: 1,
        provinceCode: provinceCode
    }
    $.ajax({
        type: 'post',
        timeout: 60000,
        url: ajaxUrlNew() + "smartfund/queryTaxCity",
        data: JSON.stringify(objTaxCity),
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
            var queryTaxInfoParams = JSON.parse(sessionStorage.getItem('queryTaxInfoParams'));
            hideLoading();
            var b = data.data;
            var cityTypeList = ""; //市列表
            if (data.errorCode == "0000") {
                $("#cityType" + index).html("");
                $("#cityType" + index).append('<option value="">请选择市/区城市</option>');
                $(b).each(function (i, m) {
                    cityTypeList += '<option value="' + m.regionCode + '">' + m.regionNameCn + '</option>';
                });
                $("#cityType" + index).append(cityTypeList);
                if (queryTaxInfoParams) {
                    if (queryTaxInfoParams.domicileCityCn) {
                        $("#cityType2").val(queryTaxInfoParams.domicileCityCn);
                    }
                    if (queryTaxInfoParams.birthplaceCityCn) {
                        $("#cityType3").val(queryTaxInfoParams.birthplaceCityCn);
                    }
                }
                $("#cityType" + index).mobiscroll().select({
                    theme: 'mobiscroll',
                    display: 'bottom',
                    headerText: function (valueText) {
                        return "请选择市/区城市";
                    },
                    onSelect: function () {
                        sel_city1 = $('#cityType2').find("option:selected").val();
                        sel_city2 = $('#cityType3').find("option:selected").val();
                        localStorage.sel_city2 = sel_city2 || localStorage.sel_city2;
                        localStorage.sel_city1 = sel_city1 || localStorage.sel_city1;
                    }
                });
            } else {
                setErrorMsg(data.respCode, data.respMsg);
            }
        },
        error: function (data) {
            hideLoading();
            if (data.statusText == "timeout") {
                errorShowAlert("请求超时");
            } else {
                errorShowAlert("请求失败，请稍后再试。");
            }
        }
    })
}
//查询涉税类型
function queryTaxType(isFlag) {
    var taxType = '';
    hideLoading();
    showLoading();
    $.ajax({
        type: 'post',
        timeout: 60000,
        url: ajaxUrlNew() + "smartfund/smartTaxType",
        data: {},
        dataType: 'json',
        success: function (data) {
            hideLoading();
            var a = data.data;
            if (data.errorCode == "0000") {
                localStorage.tax_type = "ON";
                $("#identificationType").html("");
                for (var i = 0; i < a.length - 1; i++) {
                    taxType += '<option value="' + a[i].itemValue + '">' + a[i].itemName + '</option>';
                }
                $("#identificationType").append(taxType);
                $("#identificationType").mobiscroll().select({
                    theme: 'mobiscroll',
                    display: 'bottom',
                    headerText: function (valueText) {
                        return "选择居民身份";
                    },
                    onSelect: function (valueText, inst) {
                        $('.redTip').hide();
                        tax_type = $.trim($('#identificationType').find('option:selected').val());
                        localStorage.tax_type = tax_type;
                        valueText != "仅中国税收居民" ? $('.hideDiv').show() : $('.hideDiv').hide();
                        if (valueText != "仅中国税收居民") {
                        	if(localStorage.tax_type == 'NO') {
                          		$('#provinceType2_dummy,#provinceType3_dummy').val("请选择省");
                        	}
                            $('.del_tax_info').hide();
                            if (isFlag && isFlag == 1) {
                                queryTaxCountry(1);
                            }
                            if (valueText == "仅非居民") {
                                $('.redCircle').show();
                                $('.redTip').html('中国税收居民以外的个人和企业 (包括其他组织)，但不包括政府机构、国际组织、中央银行、金融机构或者在证券市场上市交易的公司及其关联机构。<div></div>').css('width', '60%');
                                $('.redTip div').css('right', '21.8%');
                            } else {
                                $('.redCircle').hide();
                            }
//                          $('.province,.city').show();
                            $('.redTip').hide();
                            sel_con1 = "CN";
                            sel_con2 = "CN";
                            $('#countryType1_dummy,#countryType2_dummy').val("中国");
//                          $('.provinceText1,.cityText1,.provinceText2,.cityText2').hide();
                            $('.city').css("border-bottom", "none");
                            $('.city').eq(0).css("border-bottom", "1px solid #ddd");
                            if (window.location.href.indexOf("openaccountThi") < 0) {
                                if (valueText == "仅非居民" || valueText == "既是中国税收居民又是其他国家（地区）税收居民") {
                                    $('.dientification').css('border-bottom', '1px solid #ddd');
                                }
                            }
                        } else {
                            $('.redCircle').show();
                            $('.redTip').html('中国税法规定的居民企业或者居民个人。<div></div>').css('width', '41.2%');
                            $('.redTip div').css('right', '29.8%');
                            $('.dientification').css('border-bottom', 'none');
                        }
                    }
                });
            } else {
                setErrorMsg(data.errorCode, data.errorMessage);
            }
        },
        error: function (data) {
            hideLoading();
            if (data.statusText == "timeout") {
                errorShowAlert("请求超时");
            } else {
                errorShowAlert("请求失败，请稍后再试。");
            }
        }
    })
}

if (window.location.href.indexOf("openaccountFin") < 0) {
    queryTaxType(1);
}

//如果国别选择了除中国以外的别的国家则选择框变成输入框
$('#countryType1').on('change', function () {
    if ($('#countryType1').val() != "CN") {
        $(this).closest('.country').next('.province').add($(this).closest('.country').next('.province').next('.city')).hide();
        $('.cAddressInfo').hide();
        $('.provinceText1,.eAddressInfo').show();
        $(this).closest('.country').css('border-bottom', 'none');
    } else {
        $('.cAddressInfo').show();
        $(this).closest('.country').next('.province').add($(this).closest('.country').next('.province').next('.city')).show();
        $('.provinceText1,.eAddressInfo').hide();
        $(this).closest('.country').css('border-bottom', '1px solid #ddd');
    }
});
$('#countryType2').on('change', function () {
    if ($('#countryType2').val() != "CN") {
        $(this).closest('.country').next('.province').add($(this).closest('.country').next('.province').next('.city')).hide();
        $('.provinceText2,.cityText2').show();
        $('.provinceText2').css('border-bottom', 'none');
        $(this).closest('.country').css('border-bottom', 'none');
    } else {
        $(this).closest('.country').next('.province').add($(this).closest('.country').next('.province').next('.city')).show();
        $('.provinceText2,.cityText2').hide();
        $(this).closest('.country').css('border-bottom', '1px solid #ddd');
    }
});

